package com.usian.login.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.pojo.ResultVo;
import com.usian.login.dao.LoginDao;
import com.usian.login.dao.UserDao;
import com.usian.login.pojo.TbUser;
import com.usian.login.service.LoginService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserDao userDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResultVo sendCode(String phone) {
//1、判断是否是一个正确的手机号
        boolean isPhone = Validator.isMobile(phone);
        if (isPhone) {
            //2、判断手机号是否已经注册
            int count = loginDao.getUserCountByPhone(phone);
            if (count > 0) {
                //手机号已经注册
                //3、发送验证码
//                AliCodeUtils aliCodeUtils = new AliCodeUtils();
//                aliCodeUtils.sendLoginCode(phone, "123455");
//                stringRedisTemplate.opsForValue().set(phone, "123455");
//                stringRedisTemplate.expire(phone, 1,TimeUnit.MINUTES);
                rabbitTemplate.convertAndSend("java-exchange", "java-queue", phone);
                return new ResultVo(200, "短信发送成功", null);
            } else {
                return new ResultVo(303, "此手机号格不存在或尚未注册", null);
            }

        } else {
            return new ResultVo(303, "手机号格式错误", null);
        }
    }

    @Override
    public ResultVo userLogin(String phone, String code) {
        //1、判断手机号和验证码是否填写
        boolean isHavephone = StrUtil.isEmpty(phone);
        boolean isHaveCode = StringUtils.isEmpty(code);
        if (!isHavephone) {
            if (!isHaveCode) {
                //2、用户判断是否已经存在
//                int count = loginDao.getUserCountByPhone(phone);
                LambdaQueryWrapper<TbUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(TbUser::getMobile, phone);
                TbUser tbUser = userDao.selectOne(lambdaQueryWrapper);
                if (tbUser != null) {
                    //3、验证码是否正确 匹配用户输入的验证码和我们保存的验证码是否一致
                    String redisCode = stringRedisTemplate.opsForValue().get(phone);
                    if (!StrUtil.isEmpty(redisCode)) {
                        if (redisCode.equals(code)) {
                            //4、返回用户个人信息
                            return new ResultVo(200, "success", tbUser);
                        } else {
                            return new ResultVo(303, "验证码输入错误", null);
                        }
                    } else {
                        return new ResultVo(303, "验证码以超期", null);
                    }
                } else {
                    return new ResultVo(303, "此手机号不存在或尚未注册", null);

                }

            } else {
                return new ResultVo(303, "验证码必填", null);

            }
        } else {
            return new ResultVo(303, "手机号必填", null);

        }
    }

    @Override
    public ResultVo save(TbUser tbUser) {
        userDao.insert(tbUser);
        return new ResultVo(200,"success",null);
    }
}
